package edu.buffalo.son.garman;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class ICD9ToCategory {
    private static final String FILE_NAME = 
            "DIAG_CD_list.csv";
    private static HashMap<String,String> dataMap = new HashMap<>();
    private static BufferedReader reader;
    static {
        try {
            reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(FILE_NAME)));
            String line = null;

            line = reader.readLine(); // Read the header & ignore

            while((line = reader.readLine()) != null) {
                String[] params = line.split(",");
                if (params.length == 3) {

                    String formattedICD9 = getFormattedICD9Code(params[0]);

                    dataMap.put(formattedICD9, params[2]);
                }
                else {
                    //TODO logger error.
                    System.err.println(line);
                }
            }
            //printDataMap(); // TODO remove this line
            reader.close();
        } catch (FileNotFoundException e) {
            // TODO Log statement
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static void printDataMap(){
        for(String code:dataMap.keySet()) {
            System.out.println(code + " : " + dataMap.get(code));
        }
    }

    public static String lookupCategory(String ICD9Code) {
        
        ICD9Code = getFormattedICD9Code(ICD9Code);
        
        //System.out.println(ICD9Code + " : " + dataMap.get(ICD9Code));
        return dataMap.get(ICD9Code);
    }

    public static String getFormattedICD9Code(String icd9Code) {
        /*if(icd9Code.contains("481")) {
            System.out.println(icd9Code);
        }*/
        if(icd9Code == null) {
            return null;
        }
        icd9Code = icd9Code.replace(String.valueOf((char) 160), " ").trim(); // Non breaking space.
        if(icd9Code == null || icd9Code.trim().equals("")) {
            return null;
        }
        
        int dotIndex = icd9Code.indexOf(".");
        
        // Append zeros infront for upto 3 digits before . (dot)
        while( dotIndex!= -1 && dotIndex < 3) {
            icd9Code = "0" + icd9Code;
            dotIndex++;
        }
        
        icd9Code = icd9Code.replace(".", "");

        for(int i=icd9Code.length(); i < 5; i++) {
            icd9Code = icd9Code + "0";
        }
        return icd9Code;
    }

    /*    public static void main(String args[]) {
        new ICD9ToCategory();
    }
     */
}
